﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace TravelAgentsDesktopDBApp
{
    public partial class FormPackages : Form
    {
        //class vars
        private Packages _selectedPkg = null;
        private List<ProductsSuppliers> _checkedItems = new List<ProductsSuppliers>();

        public FormPackages()
        {
            InitializeComponent();
        }



        /// Validation of the input information//// added by  Qingsheng Zhou(Jason)////////

        protected bool isNumberic(string message)
        {
            System.Text.RegularExpressions.Regex rex =
            new System.Text.RegularExpressions.Regex(@"^\d+$");
            int result = -1;
            if (rex.IsMatch(message))
            {
                result = int.Parse(message);
                return true;
            }
            else
                return false;
        }

        ///////////The validation , added by Qingsheng Zhou(Jason)///////////////////
        private bool validateData()
        {
            //Packages myPackage = new Packages();

            if (double.Parse(txtAgencyComm.Text) > double.Parse(txtPkgPrice.Text))
            {
                MessageBox.Show("Agency commision can not be greater than the base price");
                return false;
            }

            if (dtpPkgStart.Value > dtpPkgEnd.Value)
            {
                MessageBox.Show(" End of Package date can not be later than the start of Package date");
                return false;
            }

            if (txtPkgName.Text == "")
            {
                MessageBox.Show("Package name can not be null");
                return false;
            }

            if (txtPkgDescription.Text == "")
            {
                MessageBox.Show("Description can not be null");
                return false;
            }

            if (!isNumberic(txtPkgPrice.Text))
            {
                MessageBox.Show("Price should be numeric");
                return false;
            }

            if (!isNumberic(txtAgencyComm.Text))
            {
                MessageBox.Show("Agency commision should be numeric");
                return false;
            }

            return true;

        }

        ///Get all the information for insert into database, added by  Qingsheng Zhou(Jason)

        private Packages PackageDetails()
        {
            Packages myPackage = new Packages();
            if (validateData())
            {
                myPackage.PackageId = int.Parse(txtPkgId.Text);
                myPackage.PkgName = txtPkgName.Text;
                myPackage.PkgStartDate = dtpPkgStart.Value;
                myPackage.PkgEndDate = dtpPkgEnd.Value;
           //     myPackage.PkgBasePrice = txtPkgPrice.Text;
                myPackage.PkgDesc = txtPkgDescription.Text;
           //     myPackage.PkgAgencyCommission = txtAgencyComm.Text;
            }

            return myPackage;
        }

        private Packages getPackage()
        {
            //added by: moses
            //added on: 12/01/22

            //function returns a package from user input
            //if user input passes validation, return package
            //else returns null and marks errors on form

            //ensure error markers are initially hidden
            foreach (Control ctrl in pnlErr.Controls)
            {
                ctrl.Visible = false;
            }

            bool isValid = true;
            StringBuilder errMsg = new StringBuilder("The following errors occurred:");
            errMsg.AppendLine().AppendLine();
            Packages package = new Packages();

            //validate the data as we try to create a package
            
            int pkgId = 0;
            if (int.TryParse(txtPkgId.Text, out pkgId)) { }
            package.PackageId = pkgId;

            foreach (Control ctrl in gpbPackages.Controls)
            {
                string ctrlName = ctrl.Name;

                switch (ctrlName)
                {
                    case "txtPkgName":

                        if (txtPkgName.Text == "")
                        {
                            isValid = false;
                            errPkgName.Visible = true;
                            errMsg.AppendLine("\tPackage name is required");
                        }

                        else
                            { package.PkgName = txtPkgName.Text; }

                        break;

                    case "dtpPkgStart":

                        if (dtpPkgStart.Value > dtpPkgEnd.Value)
                        {
                            isValid = false;
                            errEndDate.Visible = true;
                            errMsg.AppendLine("\tEnd date precedes start date");
                        }

                        else
                        {
                            package.PkgStartDate = dtpPkgStart.Value;
                            package.PkgEndDate = dtpPkgEnd.Value;
                        }

                        break;

                    case "txtPkgDescription":

                        if (txtPkgDescription.Text == "")
                        {
                            isValid = false;
                            errPkgDesc.Visible = true;
                            errMsg.AppendLine("\tPackage description is required");
                        }

                        else { package.PkgDesc = txtPkgDescription.Text; }

                        break;


                    case "txtPkgPrice":

                        decimal price;
                        decimal commish;

                        if (!Decimal.TryParse(txtPkgPrice.Text, out price))
                        {
                            isValid = false;
                            errPrice.Visible = true;
                            if (txtPkgPrice.Text == "")
                            { errMsg.AppendLine("\tPackage price is required"); }
                            else
                            { errMsg.AppendLine("\tPackage price is invalid"); }
                        }

                        else
                        {
                            package.PkgBasePrice = price;

                            if (!Decimal.TryParse(txtAgencyComm.Text, out commish))
                            {
                                isValid = false;
                                errCommish.Visible = true;
                                errMsg.AppendLine("\tCommission price is invalid");
                            }
                            else
                            {
                                if (commish > package.PkgBasePrice)
                                {
                                    isValid = false;
                                    errCommish.Visible = true;
                                    errMsg.AppendLine("\tCommission price is higher than base price");
                                }

                                else { package.PkgAgencyCommission = commish; }
                            }
                        }



                        break;


                    default: break;


                }
            }

            if (!isValid)
            {
                MessageBox.Show(errMsg.ToString(), "Form Errors Found");
                return null;
            }
            else { return package; }


        }

        ////////////////////  end of validation and package details/////////////////////////////
        private void eXITToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        ////////////////////exit application////////////////////////////////////////////////////

        private void PopulateCmb()
        {
            //Added by: Moses
            //Added on: 2012/01/20

            //function that populates the search field comboboxes with values from
            //Products and Suppliers tables

            //clear all comboboxes first
            foreach (Control ctrl in grpProducts.Controls)
            {
                if (ctrl.GetType() == typeof(ComboBox))
                    { ((ComboBox)ctrl).Items.Clear(); }
            }

            //get products and suppliers
            List<Products> products = new DbAccess().SelectProducts();
            List<Suppliers> suppliers = new DbAccess().SelectSuppliers();

            //add an option for "ALL" (no filtering)
            this.cmbSearchProducts.Items.Add("ALL");
            this.cmbSearchSuppliers.Items.Add("ALL");
            
            //add the entries to the combobox
            foreach (Products entry in products)
                { this.cmbSearchProducts.Items.Add(entry); }

            foreach (Suppliers entry in suppliers)
                { this.cmbSearchSuppliers.Items.Add(entry); }

            //populate the product selected cmb
            this.cmbSearchSelected.Items.Add("ALL");
            this.cmbSearchSelected.Items.Add("YES");
            this.cmbSearchSelected.Items.Add("NO");

        }

        private void updateCheckedList()
        {
            //function to keep a record of checked items
            int supId = 0;
            int prodId = 0;
            bool supAll = false;
            bool prodAll = false;

            List<ProductsSuppliers> oldChkList = _checkedItems;
            _checkedItems.Clear();
            
            //we want to delete the unchecked items from our list as well
            //get the filter options from the cmb so we only 
            //affect the products that are currently shown in the clb

            //get selection from sup cmb and prod cmb
            if ((cmbSearchSuppliers.SelectedIndex != -1) && ((cmbSearchSuppliers.SelectedItem).GetType() == typeof(Suppliers)))
                { supId = ((Suppliers)cmbSearchSuppliers.SelectedItem).SupplierId; }
            else { supAll = true; }

            if ((cmbSearchProducts.SelectedIndex != -1) && ((cmbSearchProducts.SelectedItem).GetType() == typeof(Products)))
                { prodId = ((Products)cmbSearchProducts.SelectedItem).ProductId; }
            else { prodAll = true; }

            if (!supAll && !prodAll)
            {
                //sorted by some supplier and product
                //keep the old ones that do not match the search criteria
                foreach (ProductsSuppliers chkItem in oldChkList)
                {
                    if ((chkItem.SupplierId != supId) && (chkItem.ProductId != prodId))
                        { _checkedItems.Add(chkItem); }
                }

                //add the new entries if any
                foreach (ProductsSuppliers chkItem in clbProdSups.CheckedItems)
                { _checkedItems.Add(chkItem); }
            }

             else if (!supAll && prodAll)
             {
                //sorted by some supplier
                //keep the old ones
                foreach (ProductsSuppliers chkItem in oldChkList)
                {
                    if (chkItem.SupplierId != supId)
                        { _checkedItems.Add(chkItem); }
                }

                //add the new entries if any
                foreach (ProductsSuppliers chkItem in clbProdSups.CheckedItems)
                        { _checkedItems.Add(chkItem); }
                }

            else if (supAll && !prodAll)
            {
                //sorted by some product
                //keep the old ones
                foreach (ProductsSuppliers chkItem in oldChkList)
                {
                    if (chkItem.ProductId != prodId)
                        { _checkedItems.Add(chkItem); }
                }

                //add the new entries if any
                foreach (ProductsSuppliers chkItem in clbProdSups.CheckedItems)
                    { _checkedItems.Add(chkItem); }
            }

            else
            {
                //all entries are fair game
                foreach (ProductsSuppliers chkItem in clbProdSups.CheckedItems)
                    { _checkedItems.Add(chkItem); }
            }
 
        }

        private void populateClbProd()
        {
            List<ProductsSuppliers> searchResults = new Search().filterProSup(this);
            List<ProductsSuppliers> yesResults = new List<ProductsSuppliers>();
            List<ProductsSuppliers> noResults = new List<ProductsSuppliers>();

            //update the chkdItemsList

            updateCheckedList();


            if (_selectedPkg == null)
            {
                if (_checkedItems.Count == 0)
                {
                    foreach (ProductsSuppliers entry in searchResults)
                    {
                        clbProdSups.Items.Add(entry, false);
                        noResults.Add(entry);
                    }
                }
                else
                {
                    foreach (ProductsSuppliers entry in searchResults)
                    {
                        if (_checkedItems.Contains(entry))
                        {
                            clbProdSups.Items.Add(entry, true);
                            yesResults.Add(entry);
                        }
                        else 
                        {
                            clbProdSups.Items.Add(entry, false);
                            noResults.Add(entry);
                        }
                    }
                }
            }

            else
            {
                if (_checkedItems.Count == 0)
                    //first time a user has populated the form
                {
                    try
                    {
                        List<ProductsSuppliers> pkgProducts = new DbAccess().getAssocProd(_selectedPkg.PackageId);

                        foreach (ProductsSuppliers entry in searchResults)
                        {
                            if (pkgProducts.Contains(entry)) 
                            {
                                clbProdSups.Items.Add(entry, true);
                                yesResults.Add(entry);
                            }
                            else 
                            {
                                clbProdSups.Items.Add(entry, false);
                                noResults.Add(entry);
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show("Database error! Cannot complete your query");
                        return;
                    }
                }

                else
                {
                    foreach (ProductsSuppliers entry in searchResults)
                    {
                        if (_checkedItems.Contains(entry)) 
                        {
                            clbProdSups.Items.Add(entry, true);
                            yesResults.Add(entry);
                        }
                        else 
                        { 
                            clbProdSups.Items.Add(entry, false);
                            noResults.Add(entry);
                        }
                    }
                }

            }
        

            //filter by selected combobox
            switch (cmbSearchSelected.SelectedIndex)
            {
                case 0:
                    //"ALL"
                    clbProdSups.Items.Clear();
                    foreach (ProductsSuppliers yesEntry in yesResults)
                        { clbProdSups.Items.Add(yesEntry, true); }
                    foreach (ProductsSuppliers noEntry in noResults)
                        { clbProdSups.Items.Add(noEntry, false); }
                    break;

                case 1:
                    //"YES"
                    clbProdSups.Items.Clear();
                    foreach (ProductsSuppliers yesEntry in yesResults)
                        { clbProdSups.Items.Add(yesEntry, true); }

                    break;

                case 2:
                    //"NO"
                    clbProdSups.Items.Clear();
                    foreach (ProductsSuppliers noEntry in noResults)
                        { clbProdSups.Items.Add(noEntry, false); }

                    break;

                default: break;
            }

        }

        private void resetPkgInfoPane()
        {
            //Added by: Moses
            //Added on: 2012/01/20

            //function that resets the package editing pane
            //clears all textboxes (except for pkgid=0), sets dtp's to today's date
            //fills the product checkedlistbox with all available products

            //clear class vars
            _selectedPkg = null;
            _checkedItems.Clear();
            

            foreach (Control ctrl in gpbPackages.Controls)
            {
                //clear all textboxes
                if (ctrl.GetType() == typeof(TextBox))
                    { ctrl.Text = ""; }

                //set all datetimepickers to today's date
                if (ctrl.GetType() == typeof(DateTimePicker))
                    { ((DateTimePicker)ctrl).Value = DateTime.Today; }

                //set all comboboxes selectedindex to 0
                if (ctrl.GetType() == typeof(GroupBox))
                {
                    foreach (Control gpbCtrl in ((GroupBox)ctrl).Controls)
                    {
                        if (gpbCtrl.GetType() == typeof(ComboBox))
                            { ((ComboBox)gpbCtrl).SelectedIndex = 0; }
                    }
                }
            }

            //hide the error markers
            foreach (Control ctrl in pnlErr.Controls)
            {
                ctrl.Visible = false;
            }



            txtPkgId.Enabled = false;
            txtPkgId.Text = 0 + ""; //hack to ensure validation passes, db auto-generates id anyway

            //fill checkedlistbox with all available products from productsSuppliers table
            populateClbProd();
            //refresh the comboboxes also
            PopulateCmb();

        }

        private void refreshPkgListPane()
        {
            //Added by: Moses
            //Added on: 2012/01/20

            //function that refreshes the package listbox with packages from db

            //clear selectedpkg
            _selectedPkg = null;
            _checkedItems.Clear();
            
            //clear the listbox and populate with all packages
            lstDisplay.Items.Clear();
            lstDisplay.Items.Add("CREATE NEW PACKAGE");
            lstDisplay.Items.Add("");
            List<Packages> pkgList = new DbAccess().SelectPackages();

            foreach (Packages entry in pkgList)
                { lstDisplay.Items.Add(entry); }

        }

        private void FormPackages_Load(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/20

            //prepare the form on load

            PopulateCmb();
            refreshPkgListPane();
            resetPkgInfoPane();
            showPkgList();
        }

        private void cmbSearchSuppliers_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/20
            //Edited on: 2012/01/21 -- added default checks to products when a package has been selected
            //Edited on: 2012/01/22 -- unified this with cmbSearchProducts_SelectedIndexChanged() to populateClbProd()

            populateClbProd();

        }

        private void cmbSearchProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/20
            //Edited on: 2012/01/21 -- added default checks to products when a package has been selected
            //Edited on: 2012/01/22 -- unified this with cmbSearchSuppliers_SelectedIndexChanged() to populateClbProd()

            populateClbProd();

        }

        private void showPkgDeets()
        {
            //Added by: Moses
            //Added on: 2012/01/21

            //function shows the package edit groupbox and hide the package list groupbox
            gpbPkgList.Visible = false;
            gpbPkgList.Enabled = false;
            btnShowPkgs.Visible = true;
            btnShowPkgs.Enabled = true;
            gpbPackages.Visible = true;
            gpbPackages.Enabled = true;
        }

        private void showPkgList()
        {
            //Added by: Moses
            //Added on: 2012/01/21

            //function shows the package list groupbox and hide package edit groupbox
            gpbPackages.Visible = false;
            gpbPackages.Enabled = false;
            btnShowPkgs.Visible = false;
            btnShowPkgs.Enabled = false;
            gpbPkgList.Visible = true;
            gpbPkgList.Enabled = true;
        }

        private void lstDisplay_Click(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/21

            //function handles a click in the package listbox
            //context-based control

            //3 types of objects stored in this listbox
            //strings, nulls, and packages
            //nulls are only used to format
            //strings are commands or package details

            //ensure something is selected since we populate the listbox with null values..
            if (lstDisplay.SelectedItem != null)
            {

                if ((lstDisplay.SelectedItem).GetType() == typeof(string))
                {
                    switch ((lstDisplay.SelectedItem).ToString())
                    {
                        //take care of any commands here
                        case "CREATE NEW PACKAGE":
                            //go to package details pane
                            btnMultiFunc.Text = "SAVE NEW";
                            btnClear.Text = "CLEAR FORM";
                            resetPkgInfoPane();
                            showPkgDeets();
                            break;

                        case "BACK TO ALL PACKAGES":
                            //show packages in the listbox
                            resetPkgInfoPane();
                            refreshPkgListPane();
                            showPkgList();
                            break;


                        case "EDIT THIS PACKAGE":
                            //go to package details pane
                            //and populate fields with selected package

                            //ensure the first item is in fact a Package
                            if (lstDisplay.Items[0].GetType() == typeof(Packages))
                            {
                                btnMultiFunc.Text = "UPDATE RECORD";
                                btnClear.Text = "RESET";

                                Packages pkg = (Packages)lstDisplay.Items[0];
                                txtPkgId.Text = pkg.PackageId + "";
                                txtPkgName.Text = pkg.PkgName;
                                dtpPkgStart.Value = pkg.PkgStartDate;
                                dtpPkgEnd.Value = pkg.PkgEndDate;
                                txtPkgDescription.Text = pkg.PkgDesc;
                                txtPkgPrice.Text = pkg.PkgBasePrice.ToString("F2");
                                txtAgencyComm.Text = pkg.PkgAgencyCommission.ToString("F2");

                                cmbSearchSelected.SelectedItem = "YES";

                                showPkgDeets();
                            }
                            else { MessageBox.Show("Could not edit.\nUnknown error!"); }

                            break;

                        case "DELETE THIS PACKAGE":
                            //ask user for delete confirmation, otherwise do nothing

                            if (_selectedPkg != null)
                            {
                                
                                //show a msg box to confirm deletion
                                DialogResult confirm = MessageBox.Show("Do you really want to delete " + _selectedPkg.PkgName + "?",
                                    "Confirm Package Deletion", MessageBoxButtons.YesNo);

                                if (confirm == DialogResult.Yes)
                                {
                                    new DbAccess().DeletePackages(_selectedPkg);
                                    resetPkgInfoPane();
                                    refreshPkgListPane();
                                    showPkgList();
                                }
                            }

                            else
                                { MessageBox.Show("Could not delete!\nUnknown error!"); }

                            break;


                        default:
                            break;

                    }
                }

                else if ((lstDisplay.SelectedItem).GetType() == typeof(Packages))
                {
                    //if a command was not selected, then get the package and
                    //display the details formatted

                    _selectedPkg = (Packages)lstDisplay.SelectedItem;
                    //a package has been selected, show package details in listbox,
                    //followed by CREATE, EDIT, DELETE, BACK commands
                    lstDisplay.Items.Clear();

                    lstDisplay.Items.Add(_selectedPkg);
                    lstDisplay.Items.Add("");
                    lstDisplay.Items.Add("DETAILS");
                    lstDisplay.Items.Add("\tID: " + _selectedPkg.PackageId);
                    lstDisplay.Items.Add("\tNAME: " + _selectedPkg.PkgName);
                    lstDisplay.Items.Add("\tDESCRIPTION: " + _selectedPkg.PkgDesc);
                    lstDisplay.Items.Add("");
                    lstDisplay.Items.Add("DATES");
                    lstDisplay.Items.Add("\tSTART: " + _selectedPkg.PkgStartDate.ToString("yyyy/MM/dd"));
                    lstDisplay.Items.Add("\tEND: " + _selectedPkg.PkgEndDate.ToString("yyyy/MM/dd"));
                    lstDisplay.Items.Add("");
                    lstDisplay.Items.Add("PRICING");
                    lstDisplay.Items.Add("\tBASE: " + _selectedPkg.PkgBasePrice.ToString("C"));
                    lstDisplay.Items.Add("\tCOMMISSION: " + _selectedPkg.PkgAgencyCommission.ToString("C"));
                    lstDisplay.Items.Add("");
                    
                    //add the products
                    lstDisplay.Items.Add("PRODUCTS");

                    List<ProductsSuppliers> listThese = new DbAccess().getAssocProd(_selectedPkg.PackageId);
                    foreach (ProductsSuppliers entry in listThese)
                    { lstDisplay.Items.Add("\t" + entry); }

                    lstDisplay.Items.Add("");
                    lstDisplay.Items.Add("");
                    
                    //add some commands

                    lstDisplay.Items.Add("EDIT THIS PACKAGE");
                    lstDisplay.Items.Add("DELETE THIS PACKAGE");
                    lstDisplay.Items.Add("BACK TO ALL PACKAGES");

                }


            }
        }

        private void btnShowPkgs_Click(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/21

            //function handles the "BACK TO PACKAGES" btn click

            resetPkgInfoPane();
            refreshPkgListPane();
            showPkgList();
        }

        private void btnMultiFunc_Click(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/21
            
            //function handles the multi-function button
            //dependent on context, button will trigger either 
            //a db insert or db update

            //this button will trigger an update or save a new package
            switch (btnMultiFunc.Text)
            {
                case "SAVE NEW":
                    Packages newCandidate = getPackage();
                    if (newCandidate != null)
                    { 
                        //insert the package
                        new DbAccess().InsertPackages(newCandidate);
                        
                        //determine which products were selected and add them too
                        List<PackagesProductsSupplier> addProdList = new List<PackagesProductsSupplier>();

                        foreach (ProductsSuppliers entry in clbProdSups.CheckedItems)
                        {
                            PackagesProductsSupplier pps = new PackagesProductsSupplier();
                            pps.PackageId = new DbAccess().getLatestPkgId();
                            pps.ProductSupplierId = entry.ProductSupplierId;
                            addProdList.Add(pps); 
                        }

                        foreach (PackagesProductsSupplier entry in addProdList)
                        {
                            new DbAccess().InsertPackagesProductsSuppliers(entry);
                        }
                        
                        refreshPkgListPane();
                        showPkgList();
                    }

                    break;

                case "UPDATE RECORD":
                    Packages updCandidate = getPackage();
                    if (updCandidate != null)
                    { 
                        new DbAccess().UpdatePackages(updCandidate);

                        List<ProductsSuppliers> curProds = new DbAccess().getAssocProd(updCandidate.PackageId);
                        List<ProductsSuppliers> newProds = new List<ProductsSuppliers>();
                        List<PackagesProductsSupplier> addProds = new List<PackagesProductsSupplier>();
                        List<PackagesProductsSupplier> delProds = new List<PackagesProductsSupplier>();

                        foreach (ProductsSuppliers entry in clbProdSups.CheckedItems)
                            { newProds.Add(entry); }


                        foreach (ProductsSuppliers entry in newProds)
                        { 
                            if (!curProds.Contains(entry)) 
                            {
                                PackagesProductsSupplier ppsAdd = new PackagesProductsSupplier();
                                ppsAdd.PackageId = updCandidate.PackageId;
                                ppsAdd.ProductSupplierId = entry.ProductSupplierId;
                                addProds.Add(ppsAdd);
                            }
                        }

                        foreach (ProductsSuppliers entry in curProds)
                        {
                            if (!newProds.Contains(entry))
                            {
                                PackagesProductsSupplier ppsDel = new PackagesProductsSupplier();
                                ppsDel.PackageId = updCandidate.PackageId;
                                ppsDel.ProductSupplierId = entry.ProductSupplierId;
                                delProds.Add(ppsDel);
                            }
                        }

                        foreach (PackagesProductsSupplier entry in delProds)
                        {
                            new DbAccess().DeletePackagesProductsSuppliers(entry);
                        }

                        foreach (PackagesProductsSupplier entry in addProds)
                        {
                            new DbAccess().InsertPackagesProductsSuppliers(entry);
                        }
                        

                        //to-do: ensure previous 2 db accesses are either both done or neither done

                        refreshPkgListPane();
                        showPkgList();

                    }

                    break;

                default: break;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //Added by: Moses
            //Added on: 2012/01/21

            //function to handle a multi-function button
            //button function is dependent on context
            //can either reset the input form, or 
            //reset the input form and enable db insert

            switch (btnClear.Text)
            {
                case "CLEAR FORM":
                    resetPkgInfoPane();
                    break;

                case "RESET":

                    //repopulate fields with default values

                    if (_selectedPkg != null)
                    {
                        txtPkgId.Text = _selectedPkg.PackageId.ToString();
                        txtPkgName.Text = _selectedPkg.PkgName;
                        txtPkgDescription.Text = _selectedPkg.PkgDesc;
                        txtAgencyComm.Text = _selectedPkg.PkgAgencyCommission.ToString("F2");
                        txtPkgPrice.Text = _selectedPkg.PkgBasePrice.ToString("F2");
                        dtpPkgEnd.Value = _selectedPkg.PkgEndDate;
                        dtpPkgStart.Value = _selectedPkg.PkgStartDate;

                        clbProdSups.Items.Clear();
                        List<ProductsSuppliers> newList = new DbAccess().getAssocProd(_selectedPkg.PackageId);
                        foreach (ProductsSuppliers entry in newList)
                            { clbProdSups.Items.Add(entry, true); }
                        cmbSearchSelected.SelectedValue = "YES";
                    }
                    break;

                default: break;
            }

        }

        private void cmbSearchSelected_SelectedIndexChanged(object sender, EventArgs e)
        {
            //filter the product checklistbox according to selected criteria
            populateClbProd();


        }


    }
}